﻿package 
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;	
	import flash.events.Event;
	import flash.geom.Matrix;
	import flash.text.TextField;
	import org.papervision3d.cameras.Camera3D;
	import org.papervision3d.lights.PointLight3D;
	import org.papervision3d.materials.BitmapMaterial;
	import org.papervision3d.materials.shadematerials.FlatShadeMaterial;
	import org.papervision3d.objects.primitives.Plane;
	import org.papervision3d.objects.primitives.Sphere;
	import org.papervision3d.render.BasicRenderEngine;
	import org.papervision3d.scenes.Scene3D;
	import org.papervision3d.view.Viewport3D;

	/*
	 * coded by moka 2009 / 07/ 24 
	 * name : moka's reflection 
	 * blog : http://designplusplus.blogspot.com
	 */
	
	public class Main extends Sprite 
	{		
		private var bg:Bitmap;
		private var scene:Scene3D;
		private var viewport:Viewport3D;
		private var camera:Camera3D;
		private var render:BasicRenderEngine;
		private var plane:Plane;
		private var sphere:Sphere;
		private var phase:Number

		public function Main():void 
		{				
			// stage setting
			stage.scaleMode = "noScale";
			
			// background
			bg = addChild(new Bitmap(new BitmapData(640, 480, false, 0x000000)))as Bitmap;
			
			// pv3d essential setting
			scene = new Scene3D();
			camera = new Camera3D();
			viewport = new Viewport3D();
			render = new BasicRenderEngine();			
			
			// create plane
			plane = scene.addChild(new Plane(new BitmapMaterial(new BitmapData(640, 480, false,0x333333),true), 640, 480, 1, 1)) as Plane;
			plane.material.oneSide = false;
			plane.rotationX = -90;	
			plane.z = -400;
			
			// create sphere
			sphere = scene.addChild(new Sphere(new FlatShadeMaterial(new PointLight3D))) as Sphere;
			sphere.y = 500;
			sphere.z = -400;
			sphere.geometry.vertices[0].y = -100;
			sphere.geometry.vertices[sphere.geometry.vertices.length - 1].y = 100;
			
			// angle control
			phase = 0;

			// Moka's work 2009
			var moka:TextField = addChild(new TextField()) as TextField;
			moka.htmlText = "<font color='#FFFFFF'>Moka's work</font>";
			
			//event registration
			stage.addEventListener(Event.ENTER_FRAME, onProcess)
		}		

		private function onProcess(e:Event):void {
			bg.bitmapData.fillRect(bg.bitmapData.rect, 0x000000);
			// control sphere
			phase+=1;
			sphere.y = 600 - mouseY;
			sphere.x = 300 * Math.cos(phase / 180 * Math.PI);
			sphere.z = -400 - (mouseX - 320);
			sphere.rotationX++;
			
			// camera position(0,-250,-400). the camera is under the plane.
			camera.x = 0;
			camera.y = plane.y - 250;
			camera.z = -400;
			camera.rotationX = -90;
			plane.visible = false; // the plane will block the line of sight, so close it.
			render.renderScene(scene, camera, viewport); 
			var bmp:BitmapData = plane.material.bitmap;
			bmp.fillRect(bmp.rect, 0x060606);
			bmp.draw(viewport);// draw current view of camera to the material of plane
			
			// camera position(0,-250,-1000) original view
			camera.x = 0;
			camera.y = 250;
			camera.z = -1000;
			camera.rotationX = 0;
			plane.visible = true;
			render.renderScene(scene, camera, viewport);
			bg.bitmapData.draw(viewport); // draw current view of camera to bg
		}
	}		
}